package javawdw.picture.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javawdw.io.simple.IOLoader;
import javawdw.picture.codec.FrameCodec;
import javawdw.picture.codec.Gif2DynCodec;
import javawdw.picture.core.DynPicture;
import javawdw.picture.core.Frame;
import javawdw.picture.exception.DecodeException;

public class PictureUtil {
	private static Gif2DynCodec gif2DynCodec = new Gif2DynCodec();
	private static FrameCodec frameCodec = new FrameCodec();

	/**
	 * 将给定的文件URL构造解码成DynPicture
	 * 
	 * @param fileUrl
	 *            给定的文件URL
	 */
	public static DynPicture decodeToDyn(String fileUrl) throws IOException,
			DecodeException {
		return decodeToDyn(IOLoader.loadFile(fileUrl));
	}

	/**
	 * 将给定文件解码成DynPicture
	 * 
	 * @param file
	 *            给定的文件
	 */
	public static DynPicture decodeToDyn(File file) throws IOException,
			DecodeException {
		return decodeToDyn(new FileInputStream(file));
	}

	/**
	 * 将给定输入流解码成DynPicture
	 * 
	 * @param inputStream
	 *            给定的输入流
	 */
	public static DynPicture decodeToDyn(InputStream inputStream)
			throws IOException, DecodeException {
		return gif2DynCodec.decode(inputStream);
	}

	/**
	 * 将给定文件解码成Frame
	 * 
	 * @param file
	 *            给定的文件
	 */
	public static Frame decodeToFrame(File file) throws IOException,
			DecodeException {
		return decodeToFrame(new FileInputStream(file));
	}

	/**
	 * 将给定的文件URL解码成Frame
	 * 
	 * @param fileUrl
	 *            给定的文件URL
	 */
	public static Frame decodeToFrame(String fileUrl) throws IOException,
			DecodeException {
		return decodeToFrame(IOLoader.loadFile(fileUrl));
	}

	/**
	 * 将给定输入流解码成Frame
	 * 
	 * @param inputStream
	 *            给定的输入流
	 */
	public static Frame decodeToFrame(InputStream inputStream)
			throws IOException, DecodeException {
		return frameCodec.decode(inputStream);
	}
}